<script>
import PDFJS from "pdfjs-dist";
import * as pdfjsLib from "pdfjs-dist";

export default {
  data() {
    return {
      pdfDoc: null,
      pdfList: 0,
      url: ''
    };
  },
  mounted() {
    this.initThePDFJSLIB();
    this.url  = "http://localhost:18888" + import.meta.env.BASE_URL + "static/pdf/CM20240408.pdf"; // 修改为您的 PDF 文件的路径
  },
  methods: {
    // 初始化pdfjs
    initThePDFJSLIB() {
      pdfjsLib.GlobalWorkerOptions.workerSrc = new URL("pdfjs-dist/build/pdf.worker.js",import.meta.url).href;
      this.loadFile(this.url);
    },
    _renderPage(num) {
      this.pdfDoc.getPage(num).then((page) => {
        let canvas = document.getElementById("the-canvas" + num);
        let ctx = canvas.getContext("2d");
        let dpr = window.devicePixelRatio || 1;
        let bsr =
          ctx.webkitBackingStorePixelRatio ||
          ctx.mozBackingStorePixelRatio ||
          ctx.msBackingStorePixelRatio ||
          ctx.oBackingStorePixelRatio ||
          ctx.backingStorePixelRatio ||
          1;
        let ratio = dpr / bsr;
        let viewport = page.getViewport(
          screen.availWidth / page.getViewport(1).width
        );
        canvas.width = viewport.width * ratio;
        canvas.height = viewport.height * ratio;
        canvas.style.width = viewport.width + "px";
        canvas.style.height = viewport.height + "px";
        ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
        let renderContext = {
          canvasContext: ctx,
          viewport: viewport,
        };
        page.render(renderContext);
        if (this.pdfList > num) {
          this._renderPage(num + 1);
        }
      });
    },
    loadFile(base64Str) {
      pdfjsLib.getDocument(base64Str).then((pdf) => {
        this.pdfDoc = pdf;
        this.pdfList = this.pdfDoc.numPages;
        this.$nextTick(() => {
          this._renderPage(1);
        });
      });
    },
  },
  components: {},
};
</script>
<style lang="scss" scoped>
canvas {
  display: block;
  border-bottom: 1px solid black;
}
</style>

<template>
  <div>
    <canvas v-for="page in pdfList" :id="'the-canvas'+page" :key="page"></canvas>
  </div>
</template>

<style scoped lang="scss">
.home-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  .left {
    display: flex;
    align-items: center;
  }
  .f {
    flex: 1;
    display: flex;
    justify-content: flex-end;
  }
  .btn {
    margin-left: 10px;
    line-height: 30px;
    height: 30px;
    border: 1px solid rgb(215, 0, 0);
    border-radius: 30px;
    min-width: 60px;
    padding: 0 10px;
    background: rgb(255, 241, 241);
    color: rgb(215, 0, 0);
  }
}
.overflow-y-scroll {
  overflow-y: scroll;
  overflow: hidden;
  overflow-y: auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  .img {
    // width: 100%;
  }
}
.operation-box {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;

  .operation {
    display: flex;
    align-items: center;
    justify-content: space-around;
    background-color: #404040;
    width: 100%;
    color: #fff;
    padding: 10px 20px;
    border-radius: 30px;
    box-sizing: border-box;

    .jump-box {
      flex: 1;
      font-size: 14px;
      display: flex;
      align-items: center;
      justify-content: center;

      .inp-box {
        margin-right: 5px;
      }
    }

    .tips {
      flex: 1;
      text-align: center;
      font-size: 14px;
    }

    .page-num-info {
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 14px;
    }

    .page-num-info,
    .home {
      width: 60px;
      text-align: center;
    }
  }
}
</style>
